package com.example.smartcity_40.lawyer.apter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartcity_40.R;
import com.example.smartcity_40.lawyer.LawyerListActivity;
import com.example.smartcity_40.lawyer.LawyerdetailsActivity;
import com.example.smartcity_40.lawyer.bean.LegalExpertiselistBean;
import com.example.smartcity_40.lawyer.bean.LegalexpertiseBean;
import com.example.smartcity_40.lawyer.bean.PreferredlawyerBean;
import com.example.smartcity_40.tool.Tool;

import java.util.List;

public class LegalExpertiselistApter extends  RecyclerView.Adapter<LegalExpertiselistApter.Myhot> {

    private Context context;
    private List<LegalExpertiselistBean.RowsBean> list;

    public LegalExpertiselistApter(Context context) {
        this.context = context;
    }

    public void setDate(List<LegalExpertiselistBean.RowsBean> list){
        this.list = list;
    }
    @NonNull
    @Override
    public LegalExpertiselistApter.Myhot onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.l_item_preferredlawyer, null);
        return new LegalExpertiselistApter.Myhot(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull LegalExpertiselistApter.Myhot holder, int position) {
        try {
            String imgUrl = list.get(position).getAvatarUrl();
            if(!TextUtils.isEmpty(imgUrl)){
                Glide.with(context).load(Tool.url+imgUrl).into(holder.avatarUrl);
            }else {
                Glide.with(context).load(Tool.url+imgUrl)
                        .error(R.drawable.waimai)
                        .into(holder.avatarUrl);

            }

            String serviceName = list.get(position).getName();
            if(!TextUtils.isEmpty(serviceName)){
                holder.name.setText(serviceName);
            }

            Integer serviceTimes = list.get(position).getServiceTimes();
            if(serviceTimes !=null ){
                holder.serviceTimes.setText("咨询人数: "+String.valueOf(serviceTimes));
            }


            String workStartAt = list.get(position).getWorkStartAt();
            if(!TextUtils.isEmpty(workStartAt)){
                holder.workStartAt.setText("年限: "+workStartAt);
            }

            Integer favorableCount = list.get(position).getFavorableRate();
            if(favorableCount !=null ){
                holder.favorableRate.setText("好评率: "+String.valueOf(favorableCount)+"%");
            }

            String legalExpertiseName = list.get(position).getLegalExpertiseName();
            if(!TextUtils.isEmpty(legalExpertiseName)){
                holder.legalExpertiseName.setText("专长: "+legalExpertiseName);
            }

            holder.relayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LawyerdetailsActivity.class);
                    intent.putExtra("lawyerid",String.valueOf(list.get(position).getId()));
                    context.startActivity(intent);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }
/*
*
*  <!-- 律师头像、名称、从业年限、咨询人数、
    法律专长、好评率和咨询按钮-->
*
* */

    class Myhot extends RecyclerView.ViewHolder{

        private final TextView name;
        private final TextView workStartAt;
        private final TextView serviceTimes;
        private final TextView legalExpertiseName;
        private final TextView favorableRate;
        private final ImageView avatarUrl;
        private final RelativeLayout relayout;

        public Myhot(@NonNull View itemView) {
            super(itemView);
            avatarUrl = itemView.findViewById(R.id.avatarUrl);
            name = itemView.findViewById(R.id.name);
            workStartAt = itemView.findViewById(R.id.workStartAt);
            serviceTimes = itemView.findViewById(R.id.serviceTimes);
            legalExpertiseName = itemView.findViewById(R.id.legalExpertiseName);
            favorableRate = itemView.findViewById(R.id.favorableRate);
            relayout = itemView.findViewById(R.id.relayout);
        }
    }
}


